//
//  HZL_travelNotesDetailPageTableViewCell.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_travelNotesDetailPageTableViewCell.h"
#import "HZL_travelNotesDetailLocationPageTableViewCell.h"
@interface HZL_travelNotesDetailPageTableViewCell ()
@property (nonatomic, retain)UIImageView *img;
@property (nonatomic, retain)UIImageView *imgWhite;
@property (nonatomic, retain)UIImageView *imgTime;
@property (nonatomic, retain)UILabel *labelTitle;
@property (nonatomic, retain)UILabel *labelTime;
@property (nonatomic, retain)UILabel *labelLine;
@property (nonatomic, retain)UIButton *areaBtn;
@property (nonatomic, retain)UILabel *labelAreaText;
@property (nonatomic, retain)UIImageView *imgLocation;
@property (nonatomic, retain)UIImageView *imgGray;

@end
@implementation HZL_travelNotesDetailPageTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.imgWhite = [[UIImageView alloc]init];
    [self.contentView addSubview:self.imgWhite];
    self.imgWhite.backgroundColor = [UIColor whiteColor];
 
    
    self.img = [[UIImageView alloc]init];
    self.img.contentMode = UIViewContentModeScaleAspectFill;
    self.img.clipsToBounds = YES;
    [self.imgWhite addSubview:self.img];

    
    self.imgTime = [[UIImageView alloc]init];
    self.imgTime.image = [UIImage imageNamed:@"time"];
    [self.imgWhite addSubview:self.imgTime];

    
    self.labelTitle = [[UILabel alloc]init];
    [self.imgWhite addSubview:self.labelTitle];
    self.labelTitle.numberOfLines = 0;
    self.labelTitle.textColor = [UIColor darkGrayColor];
    self.labelTitle.font = [UIFont systemFontOfSize:15];

    
    
    self.labelTime = [[UILabel alloc]init];
    [self.imgWhite addSubview:self.labelTime];
    self.labelTime.textColor = [UIColor colorWithRed:0.73 green:0.73 blue:0.58 alpha:1];
    self.labelTime.font = [UIFont systemFontOfSize:13];

    
    self.labelLine = [[UILabel alloc]init];
    [self.contentView addSubview:self.labelLine];
    self.labelLine.backgroundColor = [UIColor colorWithRed:0.89 green:0.87 blue:0.78 alpha:1];
  
    
    
    
    self.areaBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.areaBtn];
    [self.areaBtn addTarget:self action:@selector(areaBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.areaBtn.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.94 alpha:1];
    
    
    self.imgLocation = [[UIImageView alloc]init];
    self.imgLocation.image = [UIImage imageNamed:@"location"];
    [self.areaBtn addSubview:self.imgLocation];

    
    self.labelAreaText = [[UILabel alloc]init];
    [self.areaBtn addSubview:self.labelAreaText];
    self.labelAreaText.userInteractionEnabled = NO;
    self.labelAreaText.font = [UIFont systemFontOfSize:13];
    self.labelAreaText.textColor = [UIColor colorWithRed:0.24 green:0.73 blue:0.82 alpha:1];

    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
        self.imgWhite.backgroundColor = [UIColor blackColor];
        self.labelTitle.textColor = [UIColor whiteColor];
        self.labelTime.textColor = [UIColor whiteColor];
    }else
    {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    }
    //标题高度
    CGFloat h = [[self class]heightWithText:self.modelWaypoint.text fontSize:15 Width:WIDTH - 40];
    
    CGFloat h1 = [[self class]heightWithText:self.modelRecords.words fontSize:15 Width:WIDTH - 40];
    
    
    CGFloat imgH = (self.modelWaypoint.h).integerValue;
    CGFloat imgW = (self.modelWaypoint.w).integerValue;
    //宽高不为0 即有图片时
    if (imgH != 0 && imgW != 0) {
        
        //白色图片
        self.imgWhite.frame = CGRectMake(10, 0, WIDTH - 20,imgH * WIDTH / imgW + 10 + h + 10 + 15 + 10);
        
        //背景图片
        self.img.frame = CGRectMake(0, 0, WIDTH - 20, (self.modelWaypoint.h).integerValue * WIDTH / (self.modelWaypoint.w).integerValue);
    }
    else
    {
        
        if (self.modelRecords.picdomain != 0) {
            
            //白色图片
            self.imgWhite.frame = CGRectMake(10, 0, WIDTH - 20,255 + 10 + h1 + 10 + 15 + 10);
            
            //背景图片
            self.img.frame = CGRectMake(0, 0, WIDTH - 20, 255);
        }
        
        else{
            //白色图片
            self.imgWhite.frame = CGRectMake(10, 0, WIDTH - 20, 10 + h + 10 + 15 + 10);
        }
        
    }
    
    
    
    
    if (h != 0 ) {
        //标题
        self.labelTitle.frame = CGRectMake(10, self.img.frame.size.height + 10, WIDTH - 40, h);
        //时间图片
        self.imgTime.frame = CGRectMake(10, self.img.frame.size.height + 10 + h + 10, 15, 15);
    }
    else
    {
        //标题
        self.labelTitle.frame = CGRectMake(10, self.img.frame.size.height + 10, WIDTH - 40, h1);
        //时间图片
        self.imgTime.frame = CGRectMake(10, self.img.frame.size.height + 10 + h1 + 10, 15, 15);
    }
    
    
    
    //时间标签
    self.labelTime.frame = CGRectMake(30, self.imgTime.frame.origin.y, 200, 15);
    
    //竖线
    self.labelLine.frame = CGRectMake(self.imgWhite.frame.origin.x + 18, self.imgWhite.frame.size.height,5 , 20);
    
    
    //位置
//    
//    self.areaBtn.frame = CGRectMake(WIDTH - 20 - 100, self.imgTime.frame.origin.y - 10, 100, self.imgTime.frame.size.height + 10);
//    
//    self.areaBtn.layer.masksToBounds = YES;
//    self.areaBtn.layer.cornerRadius = 10;
//    
//    
//    self.imgLocation.frame = CGRectMake(5, 5, 20, self.imgTime.frame.size.height);
//    
//    self.labelAreaText.frame = CGRectMake(26, 5, self.areaBtn.frame.size.width - 26 , self.imgTime.frame.size.height);
}


#pragma mark - 位置按钮触发方法
- (void)areaBtnAction
{
    [self.delegate jumpToLocation:self.indexpath];
}

- (void)setModelTravelNotes:(HZL_RecommendModel *)modelTravelNotes
{
    if (_modelTravelNotes != modelTravelNotes) {
        _modelTravelNotes = modelTravelNotes;
    }
    
    
}
- (void)setModelUser:(HZL_RecommendModel *)modelUser
{
    
}
- (void)setModelWaypoint:(HZL_RecommendModel *)modelWaypoint
{
    if (_modelWaypoint != modelWaypoint) {
        _modelWaypoint = modelWaypoint;
    }
    
    //photo中有内容
    if (self.modelWaypoint.photo.length != 0) {
        //重用过后如果img被移除 重新创建一次
        if (self.img == nil) {
            self.img = [[UIImageView alloc]init];
            self.img.contentMode = UIViewContentModeScaleAspectFill;
            self.img.clipsToBounds = YES;
            [self.imgWhite addSubview:self.img];
            self.img.backgroundColor = [UIColor grayColor];
           
        }
        [self.img sd_setImageWithURL:[NSURL URLWithString:modelWaypoint.photo] placeholderImage:nil];
    }
    else
    {
        //tableView cell 重用  原来没有图的位置中会有数据  所以先从父视图移除
        [self.img removeFromSuperview];
        self.img = nil;
    }
    
    
    
    if ((self.modelWaypoint.poiName.length == 0 || self.modelWaypoint.type == nil) && !self.modelWaypoint.longitude) {
        
        [self.areaBtn removeFromSuperview];
        self.areaBtn = nil;
        
    }
    else
    {
        if (self.areaBtn == nil) {
            
            self.areaBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            [self.contentView addSubview:self.areaBtn];
            [self.areaBtn addTarget:self action:@selector(areaBtnAction) forControlEvents:UIControlEventTouchUpInside];
            self.areaBtn.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.94 alpha:1];
            
            
            
            self.imgLocation = [[UIImageView alloc]init];
            self.imgLocation.image = [UIImage imageNamed:@"location"];
            [self.areaBtn addSubview:self.imgLocation];
         
            
            self.labelAreaText = [[UILabel alloc]init];
            [self.areaBtn addSubview:self.labelAreaText];
            self.labelAreaText.userInteractionEnabled = NO;
            self.labelAreaText.font = [UIFont systemFontOfSize:13];
            self.labelAreaText.textColor = [UIColor colorWithRed:0.24 green:0.73 blue:0.82 alpha:1];
            
            
        }
        
        if (self.modelWaypoint.poiName.length != 0) {
            self.labelAreaText.text = modelWaypoint.poiName;
        }
        else
        {
            self.labelAreaText.text = [NSString stringWithFormat:@"%@ %@ %@",modelWaypoint.country, modelWaypoint.province, modelWaypoint.city];
        }
    }
    
    
    
    
    
    self.labelTitle.text = modelWaypoint.text;
    
    
    
    self.labelTime.text = modelWaypoint.local_time;
    
}


- (void)setModelRecords:(HZL_guideModel *)modelRecords
{
    if (_modelRecords != modelRecords) {
        _modelRecords = modelRecords;
    }
    
    NSString *str = [NSString stringWithFormat:@"%@%@%@",modelRecords.picdomain, @"upload/", modelRecords.picfile];
    [self.img sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
    
    self.labelTitle.text = modelRecords.words;
    self.labelTime.text = modelRecords.lastedit;
    
    
    [self.areaBtn removeFromSuperview];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
